from django.shortcuts import render
from django.http import HttpResponse, JsonResponse, HttpResponseRedirect
from django.middleware.csrf import get_token
from django.views.decorators.csrf import csrf_exempt
from django.contrib.auth.decorators import permission_required

from .forms import *
from .models import *


# Create your views here.


def root(request):
    # with open("baidu.txt", "r") as f:
    #     f = f.readline()

    # 或者把验证文件的内容写在变量直接返回
    f = "91ad9b349b0e810a2727exxxxxxxxxxxx"

    return HttpResponse(f)


def get_csrf_token(request):
    return JsonResponse({'token': get_token(request)})


@csrf_exempt
def test_csrf(request):
    if request.method == "POST" and request.POST:
        # print(request.headers)
        name = request.POST.get("name", None)
        password = request.POST.get("password", None)
        print("xxx", request.POST.get("csrf"))
        return HttpResponse(name + password)
    return render(request, "csrf_test.html", locals())


def test_add_article(request):
    if request.method == "POST" and request.post:
        form = ArticleAdminForm
    else:
        form = ArticleAdminForm

    req = request
    return render(request, "article_add.html", locals())


# @permission_required('users.add_FriendsURL')
def add_info(request):
    pre_page_url = request.GET.get("page", "/")
    phones = request.GET.get("phones", None)
    if request.method == "POST" and request.POST:
        print(request.POST)
        form = AddInfoForm(request.POST)
        if form.is_valid():
            res_data = {
                "status": 201,
                "data": {},
                "msg": "发送成功，页面即将关闭",
            }
            return JsonResponse(res_data)
    else:
        form = AddInfoForm
    return render(request, "add_info.html", locals())


def json_parse_to_html(request):
    """
    json 字符串解析到 网页
    :param request:
    :return:
    """
    pre_page_url = request.GET.get("page", "/")

    # 默认值
    qr_platform = {
        "小橘子": "0",
        "小太阳": "0"
    }

    assist_platform = {
        "雷霆": "0",
        "kz": "0"
    }

    ip_sets = {
        "192.168.1.2": "0",
        "192.168.1.3": "0"
    }

    # 查询该用户的第一条数据，如果没有使用默认值
    content_obj = FriendsURL.objects.filter(id=1).first()
    if content_obj:
        content = content_obj.json_field
        form = eval(content)

    else:
        form = {
            "qr_platform": qr_platform,
            "qr_account": "",
            "qr_password": "",
            "project_id": "",
            "assist_platform": assist_platform,
            "assist_account": "",
            "assist_password": "",
            "user_key": "",
            "platform_ck": "",
            "ip_sets": ip_sets,
            "ip_times": "",
        }
    print(form)

    if request.method == "POST" and request.POST:
        print(request.POST)

        qr_account = request.POST.get("qr_account", "")
        qr_password = request.POST.get("qr_password", "")
        project_id = request.POST.get("project_id", "")
        assist_account = request.POST.get("assist_account", "")
        assist_password = request.POST.get("assist_password", "")
        user_key = request.POST.get("user_key", "")
        platform_ck = request.POST.get("platform_ck", "")
        ip_times = request.POST.get("ip_times", "")

        qr_platform_choice = request.POST.get("qr_platform", None)
        if qr_platform_choice:
            print(qr_platform_choice)
            qr_platform_value = qr_platform.get(qr_platform_choice, None)
            if qr_platform_value:
                qr_platform[qr_platform_choice] = "1"

        assist_platform_choice = request.POST.get("assist_platform", None)
        if assist_platform_choice:
            print(assist_platform_choice)
            assist_platform_value = assist_platform.get(assist_platform_choice, None)
            if assist_platform_value:
                assist_platform[assist_platform_choice] = "1"

        ip_sets_choice = request.POST.get("ip_sets", None)
        if ip_sets_choice:
            print(ip_sets_choice)
            ip_sets_value = ip_sets.get(ip_sets_choice, None)
            if ip_sets_value:
                ip_sets[ip_sets_choice] = "1"

        content_dict = {
            "qr_platform": qr_platform,
            "qr_account": qr_account,
            "qr_password": qr_password,
            "project_id": project_id,
            "assist_platform": assist_platform,
            "assist_account": assist_account,
            "assist_password": assist_password,
            "user_key": user_key,
            "platform_ck": platform_ck,
            "ip_sets": ip_sets,
            "ip_times": ip_times,
        }

        print(str(content_dict))
        if content_obj:
            # 如果进了这里，就是更新数据，只需要更新对应字段就行
            content_obj.json_field = str(content_dict)
            content_obj.save(update_fields=["json_field"])
        else:
            # 如果进了这里就是新建，保存到对应数据库
            pass

        res_data = {
            "status": 201,
            "data": {},
            "msg": "发送成功，页面即将关闭",
        }
        return JsonResponse(res_data)
    return render(request, "json_parse_to_html.html", locals())
